import { createRouter, createWebHistory } from 'vue-router'
import VueCookie from 'vue-cookie'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/index',
      name: 'Home',      // @代表src
      component: () => import('@/views/home.vue'),
      children:[
        {
          path: '/department',
          name: 'Department',
          component: () => import('@/views/admin/dept/index.vue')
        },
        {
          path: '/registLevel',
          name: 'RegistLevelList',
          component: () => import('@/views/admin/registLevel')
        },
        {
          path: '/emp',
          name: 'EmpList',
          component: () => import('@/views/admin/emp')
        },
        {
          path: '/register',
          name: 'Register',
          component: () => import('@/views/caiwu/register')
        },
        {
          path: '/cancelRegister',
          name: 'CancelRegister',
          component: () => import('@/views/caiwu/cancel-register')
        },
        {
          path: '/register/list',
          name: 'RegisterList',
          component: () => import('@/views/caiwu/register/list')
        },
        {
          path: '/medical',
          name: 'Medical',
          component: () => import('@/views/doctor/medical')
        },
        {
          path: '/check',
          name: 'Check',
          component: () => import('@/views/doctor/check')
        },
        {
          path: '/pay',
          name: 'Pay',
          component: () => import('@/views/caiwu/pay')
        },
        {
          path: '/refund',
          name: 'Refund',
          component: () => import('@/views/caiwu/refund')
        },
        {
          path: '/check/list',
          name: 'CheckList',
          component: () => import('@/views/check/list')
        },
        {
          path: '/register/records',
          name: 'RegisterRecords',
          component: () => import('@/views/caiwu/register/records')
        },
        {
          path: '/check/result',
          name: 'CheckResult',
          component: () => import('@/views/doctor/check/result')
        },
        {
          path: '/doctor/quezhen',
          name: 'DoctorQuezhen',
          component: () => import('@/views/doctor/medical/quezhen')
        },
        {
          path: '/doctor/prescription',
          name: 'Prescription',
          component: () => import('@/views/doctor/prescription')
        },
        {
          path: '/drug',
          name: 'Drug',
          component: () => import('@/views/doctor/drug')
        }

      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login')
    }
  ]
})
router.beforeEach((to, from, next) => { // 路由拦截  后端进行判断token是否正确
  const token = VueCookie.get('token') // 获取token
  if (to.fullPath === '/login' || token) { // 判断是否在登录页
    next() // 放行
    return
  }
  next('/login') // 否则跳转登录页
})

export default router
